package xin.lhuo.we.server.handler;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import xin.lhuo.we.server.vo.ResultVo;
import xin.lhuo.we.server.enums.ResultEnum;
import xin.lhuo.we.server.exception.WeException;
import xin.lhuo.we.server.utils.ResultVoUtil;

/**
 * 全局异常处理
 *
 * @author Lhuo
 * @date 2019/5/5 11:09
 */
@Slf4j
@ControllerAdvice
public class WeExceptionHandler {

    @ExceptionHandler(Exception.class)
    @ResponseBody
    public ResultVo handlerException(Exception e) {
        log.error("获取WeException {}", e.getMessage());
        if (e instanceof WeException) {
            WeException weException = (WeException) e;
            return ResultVoUtil.error(weException.getCode(), weException.getMessage());
        }
        e.printStackTrace();
        return ResultVoUtil.error(ResultEnum.UNKNOWN_ERROR);
    }
}
